//
//  LHWelcomeViewController.m
//  OCSinaWeibo
//
//  Created by LiHong on 15/11/2.
//  Copyright © 2015年 LiHong. All rights reserved.
//

#import "LHWelcomeViewController.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "LHAccount.h"
@interface LHWelcomeViewController ()
/// 背景
@property (nonatomic,strong)UIImageView *backgroundImageView;
/// 头像
@property (nonatomic,strong)UIImageView *iconView;
/// 欢迎label
@property (nonatomic,strong)UILabel *welcomeLabel;

@property (nonatomic,strong)NSLayoutConstraint *iconViewBottomCons;
@end

@implementation LHWelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareUI];
    NSString *urlString = [LHAccount shareAccount].avatar_large;
    if (urlString) {
        
        [self.iconView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    }
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.iconViewBottomCons.constant = -([UIScreen mainScreen].bounds.size.height - 160);
    
    [UIView animateWithDuration:1.0 delay:0.1 usingSpringWithDamping:0.6 initialSpringVelocity:3 options:UIViewAnimationOptionLayoutSubviews animations:^{
        
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 animations:^{
            
            self.welcomeLabel.alpha = 1;
            
        } completion:^(BOOL finished) {
                //TODO: - 切换控制器
        }];
    }];
}
- (void)prepareUI{
    [self.view addSubview:self.backgroundImageView];
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.welcomeLabel];
    
    self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
    self.welcomeLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
    /// 背景
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    /// 头像
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:85]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:85]];
    _iconViewBottomCons = [NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:-160];
    [self.view addConstraint:_iconViewBottomCons];
    /// 欢迎归来
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.welcomeLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.welcomeLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1 constant:16]];
    
}

#pragma mark - 懒加载
- (UIImageView *)backgroundImageView{
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ad_background"]];
    }
    return _backgroundImageView;
}

- (UIImageView *)iconView{
    if (!_iconView) {
        _iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
   //     _iconView = [[UIImageView alloc]init];
        _iconView.layer.cornerRadius = 42.5;
        _iconView.layer.masksToBounds = YES;
    }
    return _iconView;
}

- (UILabel *)welcomeLabel{
    if (!_welcomeLabel) {
        _welcomeLabel = [[UILabel alloc]init];
        _welcomeLabel.text = @"欢迎归来";
        _welcomeLabel.alpha = 0;
    }
    return _welcomeLabel;
}

@end
